<template>
  <div class="fillcontain">
    <head-top></head-top>
    <div class="table_container">

      <el-button
        type="primary"
        @click="handleAdd">手动添加</el-button>
      <el-button
        type="primary"
        @click="handleAdd">批量导入</el-button>
      <!--<el-button-->
        <!--type="primary"-->
        <!--@click="handleTAdd">上传模版</el-button>-->
      <!--列表-->
      <el-table :data="userList" highlight-current-row v-loading="loading" style="width: 100%;">
        <el-table-column type="index" width="60"></el-table-column>
        <el-table-column prop="username" label="学号" sortable></el-table-column>
        <el-table-column prop="name" label="姓名" sortable></el-table-column>
        <el-table-column prop="myGroup_name" label="班级" sortable></el-table-column>
        <el-table-column prop="cardID" label="学生卡ID" sortable></el-table-column>
        <el-table-column prop="cabinet" label="柜门号" sortable></el-table-column>
        <el-table-column prop="validity" label="有效期" sortable></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="small"
              @click="handlePatch(scope.row)">编辑</el-button>
            <el-button
              size="small"
              type="danger"
              @click="doDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
        <!--<el-table-column prop="age" label="name数量" width="100" sortable>-->
        <!--</el-table-column>-->
      </el-table>
      <el-col :span="24" class="toolbar">
        <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="10" :total="total" style="float:right;">
        </el-pagination>
      </el-col>

      <el-dialog title="创建用户" :visible.sync="dialogFormVisible" width="30%">
        <el-form :model="selectTable">
          <el-form-item label="学号">
            <el-input v-model="selectTable.username"></el-input>
          </el-form-item>
          <el-form-item label="姓名">
            <el-input v-model="selectTable.name"></el-input>
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model="selectTable.password" type="password"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="createUser">确 定</el-button>
        </div>
      </el-dialog>

      <el-dialog title="修改用户" :visible.sync="dialogFormVisible1" width="30%">
        <el-form :model="patchTable">
          <el-form-item label="学号">
            <el-input v-model="patchTable.username"></el-input>
          </el-form-item>
          <el-form-item label="姓名">
            <el-input v-model="patchTable.name"></el-input>
          </el-form-item>
          <el-form-item label="密码">
            <el-input v-model="patchTable.password" type="password"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible1 = false">取 消</el-button>
          <el-button type="primary" @click="patchUser">确 定</el-button>
        </div>
      </el-dialog>

      <el-dialog title="上传" :visible.sync="dialogTFormVisible"  width="30%">
        <el-form>
          <el-form-item label="选择需要导入的文件">
            <el-upload
              class="avatar-uploader"
              :action="uploadURL"
              :on-success="handleExamSuccess"
            >
              <!--:file-list="fileList"-->
              <!--:on-success="updatefileList"-->

              <i class="el-icon-plus"></i>  <!-- vue warning 是上传列表的问题  -->
              <div slot="tip" class="el-upload__tip">支持xls等execl文件
              </div>
            </el-upload>
            <div>
              实训软件下载：<a :href="tempURL" download="realPack.zip">点击下载</a>
            </div>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogTFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="createExam">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  import headTop from './head.vue'
  import { getUserList, createUser, patchUser, userDel, uploadURL } from '@/api/api'
  export default {
    data () {
      return {
        tempURL: '',
        uploadURL: uploadURL,
        loading: false,
        filters: {
          name: ''
        },
        selectTable: {
          username: '',
          name: '',
          password: '',
          cabinet: '',
          validity: ''
        },
        patchTable: {
          username: '',
          sNumber: '',
          password: ''
        },
        userList: [],
        page: 1,
        total: 0,
        dialogFormVisible: false,
        dialogFormVisible1: false,
        dialogTFormVisible: false
      }
    },
    components: {
      headTop
    },
    created () {
      this.initData()
    },
    methods: {
      // 获取设备名称
      async initData () {
        this.loading = true
        this.selectTable = {
          username: '',
          name: '',
          password: '',
          cabinet: '',
          validity: ''
        } // 重置输入框内容
        try {
          getUserList(this.page).then((res) => {
            this.total = res.data.count
            this.userList = res.data.results  // type 看下转换下，通过store获取
            this.loading = false
          })
        } catch (err) {
          console.log('获取数据失败', err)
        }
      },
      handleCurrentChange (val) {
        this.page = val
        this.initData()
      },
      handleExamSuccess (res, file) {
//        this.imageUrl = URL.createObjectURL(file.raw)
        this.selectTable.examURL = res.name
      },
      handleAdd () {
        this.dialogFormVisible = true
      },
      handleTAdd () {
        this.dialogTFormVisible = true
      },
      async createUser () {
        this.dialogFormVisible = false
        createUser(this.selectTable).then(res => {
          if (res.status === 200) {
            this.$message({
              type: 'success',
              message: '添加成功'
            })
            this.initData()
          } else {
            this.$message({
              type: 'error',
              message: '添加失败'
            })
          }
        }).catch(error => {
          this.$message({
            type: 'error',
            message: '添加失败'
          })
          console.log('试卷添加失败', error)
        })
      },
      doDelete (index, row) {
        this.$confirm('此操作将删除该用户,相关数据均会被清除,是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.handleDelete(index, row)
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消提交'
          })
        })
      },
      async handleDelete (index, row) {
        userDel(row.id).then((res) => {
          this.$message({
            type: 'success',
            message: '删除成功'
          })
          this.DNames.splice(index, 1)
        }).catch(error => {
          this.$message({
            type: 'success',
            message: '删除成功'
          })
          this.initData()
          console.log('类型删除失败', error)
        })
      },
      handlePatch (row) {
        this.patchTable.id = row.id
        this.patchTable.username = row.username
        this.patchTable.name = row.name
        this.patchTable.password = ''
        this.dialogFormVisible1 = true
      },
      async patchUser () {
        let postData = {}
        if (this.patchTable.password) {
          postData = {
            username: this.patchTable.username,
            name: this.patchTable.name,
            password: this.patchTable.password
          }
        } else {
          postData = {
            username: this.patchTable.username,
            name: this.patchTable.name
          }
        }
        patchUser(this.patchTable.id, postData).then(res => {
          if (res.status === 200) {
            this.$message({
              type: 'success',
              message: '修改成功'
            })
            this.dialogFormVisible1 = false
            this.initData()
          } else {
            this.$message({
              type: 'error',
              message: '修改失败'
            })
          }
        }).catch(error => {
          this.$message({
            type: 'error',
            message: '修改失败'
          })
          console.log('班级修改失败', error)
        })
      }
    }
  }

</script>

<style scoped>
  .el-button {
    margin: 5px;
  }
</style>
